﻿#include "cmdparamwidget.h"
#if _MSC_VER >=1600
#pragma execution_character_set("utf-8")
#endif
QCmdParamWidget::QCmdParamWidget(QString sWidgetName,int nWorkModeType,QMap<int,QString> cmdMap,QWidget *parent) : QWidget(parent)
{
    _nWorkModeType = nWorkModeType;

    _cmdMap = cmdMap;

    _sWidgetName = sWidgetName;

    InitUI();

    void (QButtonGroup::*fn)(int) = &QButtonGroup::buttonClicked;
    connect(_pButtonGroup,fn,this,&QCmdParamWidget::onButtonClicked);

}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdParamWidget::InitUI()
{
    _pButtonGroup = new QButtonGroup(this);


    for(int key:_cmdMap.keys())
    {
        QString sButtonName = _cmdMap.value(key);

        QPushButton* l_pushbutton =  new QPushButton(sButtonName,this);
        l_pushbutton->setCheckable(true);

        l_pushbutton->setStyleSheet(
                    "QPushButton:checked { background-color: rgb(26,188,156);color:white;}"
                    "QPushButton:unchecked { background-color: rgb(255,255,255);;color:black;}"
                    "QPushButton{background-color: rgb(255,255,255);padding:5px;border-radius:5px;color:black;"
                    "border-style: outset;"
                    "border-width: 0px;border-color: gray; "
                    "font-size:20px;font-family: 'Microsoft YaHei';}"
                                    );

        _pButtonGroup->addButton(l_pushbutton,key);
    }



    QLabel* pWidgetNamalabel= new QLabel(this);
    pWidgetNamalabel->setText(_sWidgetName+"参数");
    pWidgetNamalabel->setStyleSheet("font:20px 'Microsoft YaHei';color:white");
    pWidgetNamalabel->setMaximumHeight(40);


    QGridLayout* playout = new QGridLayout(this);

    for(int i = 0; i < 16; i++)
    {
        playout->setColumnStretch(i,1);
    }


    int nNum = 0;
    int nRow = 0;
    playout->addWidget(pWidgetNamalabel,nRow,0,1,10);

    nRow++;


    for(int key:_cmdMap.keys())
    {
        playout->addWidget(_pButtonGroup->button(key),nRow,5+nNum*2,1,2);


        nNum++;

        if(nNum >= 2)
        {
            nRow++;
            nNum = 0;
        }
    }


    nRow++;
    nRow++;
}

/******************************************
* 功能:
* 输入:
* 输出:
* 返回值:
* 维护记录:
******************************************/
void QCmdParamWidget::onButtonClicked(int buttonId)
{    
    QByteArray l_data;
    l_data.append((uchar)buttonId);

    emit signal_set_cmd_value(_nWorkModeType,l_data,1);
}

